package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.domain.LabRegistration;
import com.hushao.lims.weblims.service.LabRegistrationService;
import com.hushao.lims.weblims.vo.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 930588706
 * @date 18/1/21
 */
@Controller
@RequestMapping("/labRegistration")
public class LabRegistrationController {

    @Autowired
    private LabRegistrationService labRegistrationService;

    /**
     * 根据教室名称或班级名称或教室名称模糊查询实验室登记表（分页）
     * @param teacherName
     * @param className
     * @param classroomName
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "模糊查询实验室登记表（分页）")
    @GetMapping
    private String list(@RequestParam(value = "teacherName",required = false,defaultValue = "") String teacherName,
                        @RequestParam(value = "className",required = false,defaultValue = "")String className,
                        @RequestParam(value = "classroomName",required = false,defaultValue = "")String classroomName,
                        @RequestParam(value = "async",required = false)boolean async,
                        Pageable pageable,
                        Model model)throws Exception{
        Page<LabRegistration> labRegistrationPage=labRegistrationService.listLabRegistrationByTeacherNameLikeClassNameLikeClassroomNameLike(teacherName,className,classroomName,pageable);
        List<LabRegistration> labRegistrations=labRegistrationPage.getContent();
        model.addAttribute("page",labRegistrationPage);
        model.addAttribute("labRegistrations",labRegistrations);
        return async==true?"/classrooms/labRegistrations/list ::#mainContainerRepleace":"/classrooms/labRegistrations/list";
    }

    /**
     * 获取编辑实验室登记界面
     * @param labRegistrationId
     * @param model
     * @return
     */
    @ApiOperation(value = "获取实验室登记界面")
    @GetMapping("/edit/{labRegistrationId}")
    public String editInterface(@PathVariable("labRegistrationId") Long labRegistrationId, Model model){
        LabRegistration labRegistration=labRegistrationService.getById(labRegistrationId);
        model.addAttribute("labRegistration",labRegistration);
        return "/classrooms/labRegistrations/edit";
    }

    /**
     * 修该实验室登记
     * @param labRegistration
     * @param teacherNumber
     * @param className
     * @param classroomName
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "修改实验室登记服务")
    @ResponseBody
    @PostMapping
    public ResponseData edit(LabRegistration labRegistration,String teacherNumber,String className,String classroomName)throws Exception{
        labRegistrationService.update(labRegistration,teacherNumber,className,classroomName);
        return new ResponseData(true,"修改成功！");
    }

}
